package qdu.edu.com.fushanf4.service.count.range;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import qdu.edu.com.fushanf4.utils.UtilConstants;

import java.io.IOException;
import java.util.ArrayList;


public class RangeOfCharactersNumberMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    private final Text keyOut = new Text();
    private final IntWritable valueOut = new IntWritable();
    ArrayList<Long> arrayLists = new ArrayList<>();
    long min = 1000000,max=4500000;

    @Override
    protected void map(LongWritable key, Text value, Context context) {
        String line = value.toString();
        String[] splits = line.split(UtilConstants.RegexOfString.HORIZONTAL_TABULATION);

        arrayLists.add(Long.valueOf(splits[1]));

    }

    @Override
    protected void cleanup(Context context) throws IOException, InterruptedException {
        // 判断while是否需要继续执行
        int j = 0;
        for (Long arrayList : arrayLists){
            while (arrayList < min) {
                switch (j){
                    case 0:min -=500000;
                    max -=3500000;
                    break;
                    case 1:min -=400000;
                    max -=500000;
                    break;
                    case 2:min -=90000;
                    max -=400000;
                    break;
                    case 3:min -=5000;
                    max -=90000;
                    break;
                    case 4:min -=4000;
                    max -=5000;
                    break;
                    case 5:min -=500;
                    max -=4000;
                    break;
                    case 6:min -=200;
                    max -=500;
                    break;
                    case 7:min -=200;
                    max -=200;
                    break;
                    case 8:min -=50;
                    max -=200;
                    break;
                    case 9:min -=50;
                    max -=50;
                    break;
                }
                j++;
            }
            if (arrayList <= max) {
                keyOut.set(min + "——" + max);
                valueOut.set(1);
                System.out.println(arrayList);
                context.write(keyOut, valueOut);
            }
        }
    }
}
